<?php	
	//error_reporting( 0 );	
	
	require_once('config.php');
	include_once('function.php');			
		

	// Create connection
	$con = connectDB($host,$user,$pass,$db);

	mysqli_query($con, "SET NAMES 'utf8'");			
	
	// process import
	$products = array();

	// Estimated time to process import
	$time_eta = 0;	

	if(!empty($_POST) && isset($_FILES["file"])) // 
	{		
		// Start time
		$time_start = microtime(true);

		if ( isset($_FILES["file"])) 
		{
			//if there was an error uploading the file
			if ($_FILES["file"]["error"] > 0) 
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
				die();
			}

			// move uploaded file
			move_uploaded_file($_FILES["file"]["tmp_name"],  "upload/insert.xls");

			set_include_path(get_include_path() . PATH_SEPARATOR . 'includes/Classes/');
			include 'PHPExcel/IOFactory.php';

			// This is the file path to be uploaded.
			$inputFileName = "upload/insert.xls"; 

			try {
				$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
			} catch(Exception $e) {
				die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
			}


			$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
			$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet		
			
			// Remove first array - heading title. 
			array_splice($allDataInSheet, 0, 1);			
			
			foreach($allDataInSheet as $data)
			{
				$title 	   		= $data['A'];
				$color 			= $data['B'];
				$code 			= $data['C'];
								
				
				$sql = "INSERT INTO `products`(`code`, `title`, `color`) VALUES ('$code', '$title', '$color')";
			
				//echo $sql . "<br>";
				$result = mysqli_query($con, $sql);

				$last_id = mysqli_insert_id($con);

				$sql = "INSERT INTO `product_sizes`(`product_id`, `size`, `quantity`) VALUES ($last_id,'xxxl',0),($last_id,'xxl',0),($last_id,'xs',0),($last_id,'xl',0),($last_id,'s',0),($last_id,'m',0),($last_id,'l',0)";
				
				$result = mysqli_query($con, $sql);				


				// $product_row = mysqli_fetch_row($result);
				// $product_id = $product_row[0];
								
				// if($product_id > 0)
				// {								
				//     $sql = "UPDATE `product_sizes` SET `quantity` = CASE size ";
					
				// 	if(!empty($xxxl))
				// 	{
				// 		$sql .= " WHEN 'xxxl' THEN $xxxl  ";
				// 	}
				// 	if(!empty($xxl))
				// 	{
				// 		$sql .= " WHEN 'xxl' THEN $xxl  ";
				// 	}
				// 	if(!empty($xs))
				// 	{
				// 		$sql .= " WHEN 'xs' THEN $xs  ";
				// 	}
				// 	if(!empty($xl))
				// 	{
				// 		$sql .= " WHEN 'xl' THEN $xl  ";
				// 	}		
				// 	if(!empty($s))
				// 	{
				// 		$sql .= " WHEN 's' THEN $s  ";
				// 	}		
				// 	if(!empty($m))
				// 	{
				// 		$sql .= " WHEN 'm' THEN $m ";
				// 	}			
				// 	if(!empty($l))
				// 	{
				// 		$sql .= " WHEN 'l' THEN $l ";
				// 	}			
				// 	$sql .= " ELSE `quantity` END WHERE `product_id` = $product_id "; 		
												
				// 	mysqli_query($con, $sql);						
					
				// 	// update lần 2
				// 	$sql = "UPDATE `product_sizes` SET `code` = CASE size ";
					
				// 	if(!empty($xxxl))
				// 	{
				// 		$sql .= " WHEN 'xxxl' THEN $xxxl_code  ";
				// 	}
				// 	if(!empty($xxl))
				// 	{
				// 		$sql .= " WHEN 'xxl' THEN $xxl_code  ";
				// 	}
				// 	if(!empty($xs))
				// 	{
				// 		$sql .= " WHEN 'xs' THEN $xs_code  ";
				// 	}
				// 	if(!empty($xl))
				// 	{
				// 		$sql .= " WHEN 'xl' THEN $xl_code  ";
				// 	}		
				// 	if(!empty($s))
				// 	{
				// 		$sql .= " WHEN 's' THEN $s_code  ";
				// 	}		
				// 	if(!empty($m))
				// 	{
				// 		$sql .= " WHEN 'm' THEN $m_code ";
				// 	}			
				// 	if(!empty($l))
				// 	{
				// 		$sql .= " WHEN 'l' THEN $l_code ";
				// 	}			
				// 	$sql .= " ELSE `quantity` END WHERE `product_id` = $product_id "; 
					
				// 	mysqli_query($con, $sql);	
				// }
			}

			echo "<script>alert('Sản phẩm nhập thành công !')</script>";
		}

		// End time
		$time_end = microtime(true);		
		$time_eta = ($time_end - $time_start)/60;
	}	
		
	
	/*
	echo '<pre>';
	print_r($allDataInSheet);
	echo '</pre>';
	*/	
	
	// import link, export link, setting link
	$main_site = "http://localhost/aothun2/";
	$import_link = "http://localhost/aothun2/importw/";
	$export_link = "http://localhost/aothun2/exportw/";
	$setting_link = "http://localhost/aothun2/importw";
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8"/>
		 <meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Import dữ liệu</title>
		
		<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<!-- Bootstrap theme -->
		<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">		
		<link href="css/theme.css" rel="stylesheet">		
		<script>				
			var local_site = "http://localhost/";				
			
			function submitForm()
			{				
				document.getElementById("my_form").submit();
			}
		</script>
	</head>
	<body role="document">
		 <!-- Fixed navbar -->
		<div class="navbar navbar-default navbar-fixed-top" role="navigation">
		  <div class="container">
			<div class="navbar-header">
			  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
				<span class="sr-only">Toggle navigation</span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
			  </button>
			  <a class="navbar-brand" href="<?=$main_site; ?>">Aothun.vn</a>
			</div>
			<div class="navbar-collapse collapse">
			  <ul class="nav navbar-nav">
				<li><a href="index.php">Bảng quy đổi</a></li>					
				<li><a href="<?=$import_link; ?>">Nhập kho</a></li>					
				<li><a href="<?=$export_link; ?>">Xuất kho</a></li>							
			  </ul>
			</div><!--/.nav-collapse -->
		  </div>
		</div>
		<!-- End navbar -->
				
		<div class="container theme-showcase" role="main">
			 <div class="page-header">
				<h1>Nhập sản phẩm</h1>
			  </div>

			  <?php if($time_eta > 0): ?>
			  <div class="alert alert-success">
		        Quy trình thực hiện <?=$time_eta ?> phút.
		      </div>
		  	  <?php endif; ?>

			  <p>								
				<button type="button" class="btn btn-lg btn-success" onClick="submitForm()">Import dữ liệu</button>				
			  </p>
			  
			  <!-- Input file .xls to update data -->
			  <form role="form" method="post" enctype="multipart/form-data" id="my_form" action="<?php echo $_SERVER['PHP_SELF'];?>">
				  <div class="form-group">
					<label for="exampleInputFile">Input xls</label>
					<input type="file" id="exampleInputFile" name="file">
					<input type="hidden" name="to_make_it_works">
					<p class="help-block">* Only .xls file is available</p>
				  </div>				  
			  </form>
		</div>
	</body>
</html>